package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        //1.接收数据
        log.info("添加购物车的菜或套餐为:{}",shoppingCart);
        //2.通过session获取用户信息，empId
        Long userId=(Long) session.getAttribute("empId");
        //3.构建查询条件-dish setmeal
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());//菜
        wrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());//套餐
        wrapper.eq(userId!=null,ShoppingCart::getUserId,userId);//哪个人的车车
        //4.查询菜/套餐  one
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        //5.判断-没有菜/套餐---数量=1 新建菜/套餐
        if (one==null){
            //第一次添加数据库菜的数量为1
            shoppingCart.setNumber(1);
            //指定是哪个人的购物车
            shoppingCart.setUserId(userId);
            //加入时间维护
            shoppingCart.setCreateTime(LocalDateTime.now());
            //新建菜/套餐
            shoppingCartService.save(shoppingCart);
            //返回数据
            return R.success(shoppingCart);
        }
        //6.获取原来的数量+1
        one.setNumber(one.getNumber()+1);
        //修改数据库
        shoppingCartService.updateById(one);
        //7.返回结果
        return R.success(one);
    }

    /**
     * 查看购物车里的所有信息
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R list(HttpSession session){
        //1.接收数据
        log.info("购物车查询");
        //2.获取用户ID
         Long userId = (Long) session.getAttribute("empId");
        //3.构建查询条件
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        //4.查询
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        //5.返回数据
        return R.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("clean")
    public R clean(){
        //1.获取用户ID
        Long id = BaseContext.getId();
        //2.查询这个人的购物车
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,id);
        shoppingCartService.remove(wrapper);
        //3.返回结果
        return R.success("购物车已清空");
    }

    /**
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){
        //1.接收信息
        log.info("删除的购物车信息为:{}",shoppingCart);
        //2.构建查询条件
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        wrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if (one.getNumber()==1){
            shoppingCartService.removeById(one.getId());
            return R.success("修改成功");
        }
        one.setNumber(one.getNumber()-1);
        shoppingCartService.updateById(one);
        return R.success("修改成功");
    }

}

